package com.twb.auth.controller;

import java.io.IOException;

import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.auth.entity.verifyCode.VerifyImgCodeDto;
import com.twb.auth.service.IVerifyCodeService;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.Results;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author DSY
 * @ClassName OcrController
 * @Description ocr 服务接口
 * @date 2020年8月27日 下午4:37:17
 */
@RestController
@RequestMapping("/sys/verify")
@Api(value = "VerifyCodeController", tags = "验证码 服务接口")
public class VerifyCodeController extends BaseController {

  @Autowired
  IVerifyCodeService verifyCodeService;

  /***
   * @Description: 获取图片验证码
   * @Param unionCode:
   * @return: com.sino.core.base.Result<byte [ ]>
   * @AuthorOriginally tianxinyan
   * @date 2020/8/6 13:41
   */
  @Auth(verifyToken = false, verifyURL = false)
  @ApiOperation(value = "获取图片验证码")
  @PostMapping("/getVerifyImgCode")
  public Result<VerifyImgCodeDto> getVerifyImgCode(
      @ApiParam(value = "图片宽度", name = "width")
          Integer width,
      @ApiParam(value = "图片高度", name = "height")
          Integer height) throws IOException {
    //设置长宽
    VerifyImgCodeDto verifyCode = verifyCodeService.generate(width, height);
    return Results.successWithData(verifyCode);
  }

  /***
   * @Description: 验证图片验证码
   * @Param unionCode:
   * @Param code:
   * @return: com.sino.core.base.Result<java.lang.String>
   * @AuthorOriginally tianxinyan
   * @date 2020/8/6 13:40
   */
  @Auth(verifyToken = false, verifyURL = false)
  @ApiOperation(value = "验证图片验证码")
  @PostMapping("/checkVerifyImgCode")
  public Result<String> checkVerifyImgCode(
      @ApiParam(value = "唯一码", name = "unionCode")
      @NotBlank(message = "唯一码不能为空") @RequestParam String unionCode,
      @ApiParam(value = "验证码", name = "code")
      @NotBlank(message = "验证码不能为空") @RequestParam String code
  ) throws IOException {
    Boolean result = verifyCodeService.verifyImgCode(unionCode, code);
    if (!result) {
      return Results.failure("验证码错误，请重新输入！");
    }
    return Results.success("验证通过！");
  }
}
